package com.qing.cheng.controller;

import com.qing.cheng.dto.ResponseMessage;
import com.qing.cheng.dto.SuccessRes;
import com.qing.cheng.service.UserInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 登陆接口
 */
@RequestMapping("/login")
@RestController
public class LoginController {

    private final UserInfoService userInfoService;

    private static final Logger log = LoggerFactory.getLogger(LoginController.class);

    public LoginController(UserInfoService userInfoService) {
        this.userInfoService = userInfoService;
    }

    @RequestMapping("/getVerfiyNum")
    public ResponseMessage getVerfiyNum(@RequestParam("phoneNo") String phoneNo, HttpServletRequest request) {
        // TODO 生产验证码
        request.getSession().setAttribute("verfiyNum", 888888);
        //TODO 发送验证码
        return SuccessRes.success("SUCCESS");
    }

    @RequestMapping("/toLogin")
    public ResponseMessage toLogin(@RequestParam("phoneNo") String phoneNo, @RequestParam("verfiyNum") Integer verfiyNum, HttpServletRequest request) {
        try{
            int id = userInfoService.toLogin(phoneNo, verfiyNum, request);
            return SuccessRes.success(id);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());

        }
        return null;
    }

}
